<?php

namespace app\api\controller;
use app\admin\model\Article;
use app\common\controller\Api;
use app\common\model\Participation;
use app\manystore\model\ManystoreShop;

/**
 * 门店
 */
class Activitie extends Api
{
    protected $noNeedLogin = ['hot_activitie'];
    protected $noNeedRight = '*';
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Activities();
    }

    //报名活动
    public function addActivity(){
        $data=input('post.');
        if($data['shop_id']==""||$data['shop_id']==null){
            $this->error('请选择门店', []);
        }
        if($data['activity_id']==""||$data['activity_id']==null){
            $this->error('请选择活动', []);
        }
        if($data['phone']==""||$data['phone']==null){
            $this->error('请输入手机号', []);
        }
        if (!isValidPhoneNumber($data['phone'] )) {
            $this->error(__('手机号格式不正确'));
        }
        $result = Participation::create([
            'user_id' => $this->auth->id,
            'activity_id' => $data['activity_id'],
            'shop_id' => $data['shop_id'],
            'phone'=>$data['phone'],
        ]);
        $this->success('提交成功', []);
    }

    /*
     * 活动列表
     */
    public function activitie_list(){
        $type = input('type');
        // 如果 type 等于 1，查询已报名的活动
        if ($type == 1) {
            $participationIds = Participation::where('user_id', $this->auth->id)->column('activity_id'); // 获取当前用户参与的活动 ID
            // 如果没有找到已报名的活动，返回空列表
            if (empty($participationIds)) {
                $list = [];
            } else {
                // 查询已报名的活动
                $list = $this->model
                    ->where('status', 1)
                    ->where('pla_status', 1)
                    ->where('sh_status','<>' ,0)
                    ->whereIn('id', $participationIds)
                    ->where('shop_id',input('shop_id'))
                    ->field('id,title,image_url,start_time,end_time')
                    ->order('weigh desc')
                    ->paginate(10);
            }
        } else {
            // 默认查询所有活动
            $list = $this->model->where('status', 1)->where('pla_status', 1)->where('sh_status', '<>' ,0)->where('shop_id',input('shop_id'))->field('id,title,image_url,start_time,end_time')->order('weigh desc')->paginate(10);
        }
        foreach ($list as $item) {

            $item['time_range'] = timesf($item['start_time'],$item['end_time']); // 设置时间范围字段
        }
        $this->success('请求成功',$list);
    }


    /*
     * 首页活动
     */
    public function hot_activitie(){
        $list = $this->model->where('status', 1)->where('pla_status', 1)->where('sh_status', '<>' ,0)->where('shop_id',input('shop_id'))->where('sh_status','<>', 0)->field('id,title,image_url')->where('shop_id','=',input('shop_id'))->where('recommend',1)->order('weigh desc')->select();
        $this->success('请求成功',$list);
    }

    /*
     * 活动详情
     */

    public function activitie_info(){
        if (!$id = input('id/d')) {
            $this->error(__('请选择记录'));
        }
        $info=$this->model->where('id','=',$id)->field('id,slideshow_urls,description,video_url')->find();
        if(!$info){
            $this->success('无数据',$info);
        }
        $imagesArray = explode(',', $info['slideshow_urls']);
        $resultArray = [];
        foreach ($imagesArray as $url) {
            $resultArray[] = ['image' => trim($url)]; // 使用 trim 去除多余的空格
        }
        $info['slideshow_urls']=$resultArray;
        if($info['video_url']!=''){
            $info['video_url']=explode(',', $info['video_url']);
        }else{
            $info['video_url']=[];
        }

        $pd= Participation::where('user_id','=',$this->auth->id)->where('activity_id','=',$id )->find();
        if($pd){
            $info['in']=1;
            $shop=ManystoreShop::where('id','=',$pd['shop_id'])->field('name,tel,address')->find();
            $info['shop']=$shop;
            $info['intime']=date('Y-m-d H:i:s', $pd['createtime']);
            $info['phone']=$pd['phone'];
        }else{
            $info['in']=0;
        }

        $this->success('请求成功',$info);
    }




    public function service_notes()
    {
        $info = Article::where('type', '=', 'notes')->field('content')->find();
        $this->success('请求成功', $info);
    }



}
